/*
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

import Foundation
import UIKit

extension UIImage {

  func forceSameOrientation() -> UIImage {
    // Already oriented upright, so no need to convert
    if self.imageOrientation == UIImage.Orientation.up {
      return self
    }
    UIGraphicsBeginImageContext(self.size)
    self.draw(in: CGRect(origin: CGPoint.zero, size: self.size))
    guard let image = UIGraphicsGetImageFromCurrentImageContext() else {
      UIGraphicsEndImageContext()
      return self
    }
    UIGraphicsEndImageContext()
    return image
  }
}
